import {
  Body,
  Controller,
  Get,
  HttpException,
  HttpStatus,
  Inject,
  Logger,
  Post,
  Req,
  Request,
  Res,
  Session,
  UnauthorizedException,
  UseGuards,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { LoginDTO} from "common/lib/dto/login.dto"
import { AuthService} from "../service/auth.service"



@Controller('auth')
export class AuthController {

  private readonly logger = new Logger(AuthController.name);

  constructor(
    private readonly authService: AuthService,
    ) {}



  // 登录测试
  @UseGuards(AuthGuard('local')) //本地策略，传递local，执行local里面的validate方法
  @Post('login')
  async login(@Req() req, @Body()  body : LoginDTO, @Session() session) {
  console.log("🚀 ~ AuthController ~ login ~ body:", body)

    //通过req可以获取到validate方法返回的user，传递给login，登陆
    return this.authService.login(req.user);
  }


    // 测试登录后才可访问的接口，在需要的地方使用守卫，可保证必须携带token才能访问
    @Get('user')
    @UseGuards(AuthGuard('jwt')) //jwt策略，身份鉴权
    //通过req获取到被验证后的user，也可以使用装饰器
    getProfile(@Request() req) {
      console.log('req', req.user);
      return req.user;
    }


}
